﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOAuction;
using System.Data;

namespace DAOAuction
{
	/// <summary>
	/// author: hqlam
	/// </summary>
	public class DAOThongTinDangKy
	{
		private static LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);

		public static bool themThongTinDangKy(int maSinhVien, int maLop)
		{
			try
			{
				DANGKIHOC_KETQUA dkh_kq = new DANGKIHOC_KETQUA { MASINHVIEN = maSinhVien, MALOP = maLop };
				db.DANGKIHOC_KETQUAs.InsertOnSubmit(dkh_kq);
				db.SubmitChanges();
				return true;
			}
			catch
			{
				throw;
			}
		}

		public static List<LOP> layDanhSachLopDaDangKy(int maSinhVien)
		{
			try
			{
				List<LOP> ret = new List<LOP>();
				var dks = db.DANGKIHOC_KETQUAs.Where(dkh => dkh.MASINHVIEN == maSinhVien);
				foreach (var lopDK in dks)
					ret.Add(DAOLop.layThongTinLop(lopDK.MALOP));
				return ret;
			}
			catch
			{
				throw;
			}
		}

		public static DataTable layDanhSachLopDaDangKy(int maSinhvien, int namHoc, int hocKy)
		{
			var lst = db.DANGKYHOC_LayDanhSachLopDaDangKy(maSinhvien, namHoc, hocKy);

			DataTable res = new DataTable("RegisterList");
			res.Columns.Add("stt", typeof(int));
			res.Columns.Add("mamonhoc", typeof(int));
			res.Columns.Add("tenmonhoc", typeof(string));
			res.Columns.Add("tinchi", typeof(int));
			res.Columns.Add("lop", typeof(string));
			res.Columns.Add("dadangky", typeof(int));
			res.Columns.Add("sisotoida", typeof(int));
			res.Columns.Add("malop", typeof(int));

			int index = 1;
			foreach (var item in lst)
			{
				DataRow row = res.NewRow();
				row["stt"] = index;
				row["mamonhoc"] = item.mamonhoc;
				row["tenmonhoc"] = item.tenmonhoc;
				row["tinchi"] = item.sotinchi;
				row["lop"] = item.tenlop;
				row["dadangky"] = item.siso;
				row["sisotoida"] = item.sisotoida;
				row["malop"] = item.malop;
				res.Rows.Add(row);
				index++;
			}

			return res;
		}

		public static DataTable layDanhSachLopCoTheDangKy(int maSinhvien, int namHoc, int hocKy)
		{
			var lst = db.DANGKYHOC_LayDanhSachLopCoTheDangKy(maSinhvien, namHoc, hocKy);

			DataTable res = new DataTable("RegisterList");
			res.Columns.Add("stt", typeof(int));
			res.Columns.Add("mamonhoc", typeof(int));
			res.Columns.Add("tenmonhoc", typeof(string));
			res.Columns.Add("tinchi", typeof(int));
			res.Columns.Add("lop", typeof(string));
			res.Columns.Add("dadangky", typeof(int));
			res.Columns.Add("sisotoida", typeof(int));
			res.Columns.Add("malop", typeof(int));

			int index = 1;
			foreach (var item in lst)
			{
				DataRow row = res.NewRow();
				row["stt"] = index;
				row["mamonhoc"] = item.mamonhoc;
				row["tenmonhoc"] = item.tenmonhoc;
				row["tinchi"] = item.sotinchi;
				row["lop"] = item.tenlop;
				row["dadangky"] = item.siso;
				row["sisotoida"] = item.sisotoida;
				row["malop"] = item.malop;
				res.Rows.Add(row);
				index++;
			}

			return res;
		}

		public static bool huyDangKy(int maSinhVien, int maLop)
		{
			try
			{
				DANGKIHOC_KETQUA dkh = db.DANGKIHOC_KETQUAs.First(dkh_kq => dkh_kq.MASINHVIEN == maSinhVien && dkh_kq.MALOP == maLop);
				db.DANGKIHOC_KETQUAs.DeleteOnSubmit(dkh);
				db.SubmitChanges();
				return true;
			}
			catch
			{
				throw;
			}
		}
	}
}
